package com.ruoyi.course.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.student.domain.StudentInfo;
import com.ruoyi.student.service.IStudentInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 课程信息Controller
 * 
 * @author chenxl
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/course/course")
public class CourseController extends BaseController
{
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IStudentInfoService studentInfoService;

    /**
     * 查询课程信息列表
     */
   // @PreAuthorize("@ss.hasPermi('course:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(Course course)
    {
        System.out.println("------------");
        System.out.println("接收的参数"+ course);
        System.out.println("------------");
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    /**
     * 导出课程信息列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:export')")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course)
    {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程信息数据");
    }

    /**
     * 获取课程信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(courseService.selectCourseById(id));
    }

    /**
     * 新增课程信息
     */
    @PreAuthorize("@ss.hasPermi('course:course:add')")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course)
    {
        course.setId(IdUtils.fastSimpleUUID());
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程信息
     */
    @PreAuthorize("@ss.hasPermi('course:course:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程信息
     */
    @PreAuthorize("@ss.hasPermi('course:course:remove')")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(courseService.deleteCourseByIds(ids));
    }

    /**
     *导入课程信息
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        List<Course> courseList = util.importExcel(file.getInputStream());
        String message = courseService.importCourse(courseList, updateSupport);
        return AjaxResult.success(message);
    }

    //获取课程列表
    @GetMapping("/courselist")
    public TableDataInfo selectCourseList() {
        // 获取学院ID列表
        List<String> collegeCodes = studentInfoService.selectCollegeCodesByDeptIds();
        System.out.println("获取到的collegeCodes列表"+ collegeCodes);
        // 分页处理（建议放在判断前，确保两种分支都分页）
        //0startPage();

        List<Course> list;
        // 处理collegeIds为null或空列表的情况
        if (CollectionUtils.isEmpty(collegeCodes)) {
            // 无学院ID限制，查询全部
            list = courseService.selectCourseList(new Course());
            System.out.println("selectCourseList无学院ID限制，查询全部"+ list);
        } else {
            // 根据学院ID列表查询
            list = courseService.getCoursesByCollegeIdsAndNatureOne(collegeCodes);
            System.out.println("selectCourseList根据学院ID列表查询"+ list);
        }

        return getDataTable(list);
    }
    /*
     * 根据获取到的学院id列表获取课程列表
     */
    @GetMapping("/courselistbycollegeidlist")
    public TableDataInfo selectCourseListByCollegeIdList() {
        // 获取学院ID列表
        List<String> collegeCodes = studentInfoService.selectCollegeCodesByDeptIds();
        System.out.println("获取到的collegeCodes列表"+ collegeCodes);
        // 分页处理（建议放在判断前，确保两种分支都分页）
        //startPage();

        List<Course> list;
        // 处理collegeIds为null或空列表的情况
        if (CollectionUtils.isEmpty(collegeCodes)) {
            // 无学院ID限制，查询全部
            list = courseService.selectCourseList(new Course());
            System.out.println("selectCourseListByCollegeIdList无学院ID限制，查询全部"+ list);
        } else {
            // 根据学院ID列表查询
            list = courseService.getCoursesByCollegeIdList(collegeCodes);
            System.out.println("selectCourseListByCollegeIdList根据学院ID列表查询"+ list);
        }
        return getDataTable(list);
    }
}
